﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Linq;
using EBAYVN.DOMAIN;
using EBAYVN.Util;


namespace EBAYVN.DAO
{
    public  class KhachHangDao
    {
        private readonly EBAYVNDOMAINDataContext _db;
        public static readonly string Connectionstring = ConfigurationManager.AppSettings[Constants.SETTINGS_CONNECTION];
        public KhachHangDao()
        {
            _db = new EBAYVNDOMAINDataContext(Connectionstring);
        }
        
        public KHACHHANG    Get(int ma)
        {
            return _db.KHACHHANGs.Where(p => p.MAKH.Equals(ma)).SingleOrDefault();
        }
        public KHACHHANG Get(KHACHHANG kh)
        {
            return _db.KHACHHANGs.Where(p => p.PASSWORD.Equals(kh.PASSWORD) && p.USERNAME.Equals(kh.USERNAME)).SingleOrDefault();
        }
        
        public Message Insert(KHACHHANG objUi)
        {
            Message msg;
            try
            {
                objUi.USERNAME = objUi.USERNAME;
                objUi.PASSWORD = CommonUtil.Encrypt(objUi.PASSWORD,true);
                _db.KHACHHANGs.InsertOnSubmit(objUi);
                UserAdmin user = new UserAdmin
                {
                    Active=true,
                    
                    Username= objUi.USERNAME,
                    Password=objUi.PASSWORD,
                    HoTen=objUi.HOTEN,
                    Email=objUi.EMAIL,
                    DienThoai=objUi.DIENTHOAI,
                    DiaChi=objUi.DIACHI,
                    Deleted=false,
                    CreateDate=DateTime.Now,
                    MANV = "admin",
                };
                _db.UserAdmins.InsertOnSubmit(user);
                _db.SubmitChanges();
                msg = new Message(MessageConstants.I_CREATE_SUCCEED, MessageType.Info, "Khách Hàng");
            }
            catch (Exception ex)
            {
                // rollback transaction

                msg = ExceptionHandler.HandleInsertException(ex, "Khách Hàng", objUi.HOTEN);
            }
            return msg;
        }
        public Message Update(KHACHHANG objUi)
        {
            Message msg=null;
            try
            {
                var kh = Get(objUi.MAKH);
                if (kh != null)
                {
                    kh.HOTEN = objUi.HOTEN;
                    kh.USERNAME = objUi.USERNAME;
                    kh.DIENTHOAI = objUi.DIENTHOAI;
                    kh.DIACHI = objUi.DIACHI;
                    _db.SubmitChanges();
                    msg = new Message(MessageConstants.I_UPDATE_SUCCEED, MessageType.Info, "Khách Hàng");
                }
                
            }
            catch (Exception ex)
            {
                // rollback transaction

                msg = ExceptionHandler.HandleInsertException(ex, "Khách Hàng", objUi.HOTEN);
            }
            return msg;
        }
        public Message ChangePass(KHACHHANG objUi)
        {
            Message msg = null;
            try
            {
                var kh = Get(objUi.MAKH);
                if (kh != null)
                {
                    kh.PASSWORD = objUi.PASSWORD;
                    _db.SubmitChanges();
                    msg = new Message(MessageConstants.I_UPDATE_SUCCEED, MessageType.Info, "Khách Hàng");
                }

            }
            catch (Exception ex)
            {
                // rollback transaction

                msg = ExceptionHandler.HandleInsertException(ex, "Khách Hàng", objUi.HOTEN);
            }
            return msg;
        }
        public KHACHHANG GetByUser(string username)
        {
            return _db.KHACHHANGs.Where(p => p.USERNAME.Equals(username)).SingleOrDefault();
        }
        public List<KHACHHANG> GetListByUser(string username)
        {
            return _db.KHACHHANGs.Where(p => p.USERNAME.Equals(username)).ToList();
        }
        public KHACHHANG GetByEmail(string email)
        {
            return _db.KHACHHANGs.Where(p => p.EMAIL.Equals(email)).SingleOrDefault();
        }
    }
}
